package com.guducat.collegeWeb.config;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
import io.swagger.v3.oas.annotations.info.Contact;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.info.License;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.security.SecurityScheme;
import io.swagger.v3.oas.annotations.servers.Server;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.Paths;
import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * OpenAPI配置类
 * 用于配置API文档的基本信息和安全方案
 */
@Configuration
@OpenAPIDefinition(
        info = @Info(
                title = "大学新闻网 API",
                version = "1.0",
                description = "大学新闻网后端API文档，提供新闻管理、用户认证和首页内容管理等功能",
                contact = @Contact(
                        name = "Guducat",
                        url = "https://gitee.com/Guducat233/Collage-News-Network",
                        email = "3022146313@qq.com"
                ),
                license = @License(
                        name = "MIT License",
                        url = "https://opensource.org/licenses/MIT"
                )
        ),
        servers = {
                @Server(
                        url = "http://localhost:8080",
                        description = "本地开发环境"
                )
        },
        security = {
                @SecurityRequirement(name = "satoken")
        }
)
@SecurityScheme(
        name = "satoken",
        type = SecuritySchemeType.APIKEY,
        in = SecuritySchemeIn.HEADER,
        paramName = "satoken",
        description = "Sa-Token认证令牌，登录后在请求头中添加"
)
public class OpenApiConfig {

    /**
     * 自定义OpenAPI配置，解决Spring Boot 3.4.4与SpringDoc的兼容性问题
     */
    @Bean
    public OpenApiCustomizer openApiCustomizer() {
        return openApi -> {
            // 确保所有路径都有有效的操作对象
            Paths paths = openApi.getPaths();
            if (paths != null) {
                for (Map.Entry<String, PathItem> entry : paths.entrySet()) {
                    PathItem pathItem = entry.getValue();

                    // 检查并修复GET操作
                    fixOperation(pathItem.getGet());

                    // 检查并修复POST操作
                    fixOperation(pathItem.getPost());

                    // 检查并修复PUT操作
                    fixOperation(pathItem.getPut());

                    // 检查并修复DELETE操作
                    fixOperation(pathItem.getDelete());
                }
            }
        };
    }

    /**
     * 修复操作对象，确保所有必要的字段都存在
     */
    private void fixOperation(Operation operation) {
        if (operation != null && operation.getResponses() == null) {
            // 如果响应为空，添加一个默认响应
            operation.setResponses(new io.swagger.v3.oas.models.responses.ApiResponses());
        }
    }
}
